﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblSupplierDAO
    {
        private DBService _db = null;

        public TblSupplierDAO(String connectionString)
        {
            _db = new DBService(connectionString);
        }

        public DataTable InUpSupplier(TblSuppliers suppliers, int mode)
        {
            DataTable flag = null;
            try
            {
                flag = _db.ExeSP_DataTable("sp_TblSupplier_InsUp",
                    new string[] { "@IDSupplie", "@Suppliers", "@ImageURL", "@SuppDescription", "@Mode" },
                    new object[] { suppliers.IDSupplie, suppliers.Suppliers, suppliers.ImageURL, suppliers.SuppDescription, mode }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // kiem tra
        public bool check_suppliers(string Suppliers)
        {
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblSuppliers where Suppliers = @Suppliers",
                   new string[] { "@Suppliers" },
                   new object[] { Suppliers },
                   CommandType.Text);
                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }



        public TblSuppliers GetSuppliersByID(int IDSupplie)
        {
            TblSuppliers suppliers = new TblSuppliers();
            try
            {
                DataTable dt = null;
                dt = _db.ExeSP_DataTable("select * from TblSuppliers where IDSupplie = @IDSupplie",
                    new string[] { "@IDSupplie" },
                    new object[] { IDSupplie }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    //cate.CateID = CateID;
                    suppliers.IDSupplie = IDSupplie;
                    suppliers.Suppliers = dr["Suppliers"].ToString();
                    suppliers.SuppDescription = dr["SuppDescription"].ToString();
                    suppliers.ImageURL = dr["ImageURL"].ToString();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return suppliers;
        }

        public DataTable GetAll()
        {
            DataTable Supp = null;
            try
            {
                Supp = _db.ExeSP_DataTable("select * from TblSuppliers", false, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Supp;
        }

        public DataTable GetSuppByID(int IDSupplie)
        {
            DataTable Supp = null;
            try
            {
                Supp = _db.ExeSP_DataTable("select * from TblSuppliers where IDSupplie = @IDSupplie",
                    new string[] { "@IDSupplie" },
                    new object[] { IDSupplie }, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Supp;
        }

        public DataTable GetMenuChild(int IDSupplie)
        {
            DataTable Supp = null;
            try
            {
                Supp = _db.ExeSP_DataTable("select * from TblChildMenu where IDSupplie = @IDSupplie ",
                    new string[] { "@IDSupplie" },
                    
                    new object[] {IDSupplie},CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Supp;
        }
    }
}
